package com.medical.lsq.controller;

import com.github.pagehelper.PageInfo;
import com.medical.lsq.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.medical.lsq.entity.SysArea;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/area")

public class AreaController {

    @Autowired
    private AreaService areaService;
/**
 *分页获取行政区域列表
 *@param currentPage
 * @param limit
 * 	 @param request
 * 	  @param model
 * 	  @return
 */
    @RequestMapping("/getAreaList")
    public String getAreaList(Integer currentPage, Integer limit, HttpServletRequest request, Model model){
        String areaName = request.getParameter("context");
        currentPage = currentPage !=null ? currentPage : 1;
        limit = limit != null ? limit : 10;
        PageInfo<SysArea>pageInfo =areaService.getAllArea(areaName, currentPage, limit);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("context",areaName);
        System.out.println("pageInfo;"+pageInfo);
        System.out.println("context"+areaName);
        return "/area/area_list";
    }
    @RequestMapping("/openAdd")
    public String openApp(Model model) {return "/area/area_add";}

    @RequestMapping("/openEdit")
    public String openEdit(Integer id,Model model) {
        SysArea area =areaService.getAreaById(id);

        model.addAttribute("area",area);
        return "/area/area_edit";
    }
    @RequestMapping("/add")
    public String add(SysArea area, HttpServletRequest request, Model model) {
        areaService.add(area);
        return getAreaList(1, 10, request, model);
    }
    @RequestMapping("edit")
    public String edit(SysArea area, HttpServletRequest request, Model model) {
        areaService.edit(area);
        return getAreaList(1, 10, request, model);
    }
    @RequestMapping("delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        areaService.delete(id);
        return getAreaList(1, 10, request,model);
    }

    @ResponseBody
    @RequestMapping("getAreaTreeList")
    public Object getAreaTreeList() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("areaList", areaService.getAreaTree(""));
        return map;
    }

    }
